<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class UserSourceViewForm extends OptionViewForm
{

                        
    public  $attributeLabels;

    public $valid;

    public $userSource;

    
    public function rulesUserSource($type)
    {
        $this->type = $type;
        $valid = array(
        array("{$type}DbName, {$type}UserName, {$type}Password, {$type}DbAddress", 'required'),
        array("{$type}TablePrefix", "judgeTable"),
        array("{$type}DbAddress", "judgeConnect"),
        );
        $this->valid = $valid;
    }

    public function rules(){
        return $this->valid;
    }


    public function judgeTable(){

    }

    
    public function judgeConnect(){

        if(!$this->hasErrors())
        {
            $dbAddress = $this->{$this->type."DbAddress"};
            $userName  = $this->{$this->type."UserName"};
            $password  = $this->{$this->type."Password"};

                        try {
                $link = @mysql_connect($dbAddress, $userName, $password);
            } catch (Exception $e) {
                $this->addError($this->type.'Password','数据库连接失败');
                return;
            }
            if(!$link){
                $this->addError($this->type.'Password','数据库连接失败');
                return;
            }

            $dbName = $this->{$this->type."DbName"};
            $dbUse = @mysql_select_db($dbName, $link);
            if (!$dbUse){
                $this->addError($this->type.'DbName','没有这个数据库');
                return;
            }

                        $tablePrefix = $this->{$this->type."TablePrefix"};
            $rs = @mysql_query($this->obtainSqlUserDb());
            if(!$rs){
                $this->addError($this->type.'TablePrefix','没有这个数据表');
                return;
            }
        }

    }

    
    private function obtainSqlUserDb(){
        $sql = "";
        $tabPre = $this->{$this->type."TablePrefix"};
        if ($this->userSource == "wordpress"){
            $sql = "select * from ".$tabPre."users where 1=0";
        }elseif($this->userSource == "phpwind"){
            $sql = "select * from ".$tabPre."members where 1=0";
        }elseif($this->userSource == "discuz"){
            $sql = "select * from ".$tabPre."ucenter_members where 1=0";
        }
        return $sql;
    }

    
    public function getDataSource($type){
        $this->attributeLabels = array(
            "{$type}DbAddress"=>'数据库地址',
            "{$type}UserName"=>'登录用户名',
            "{$type}Password"=>'登录密码',
            "{$type}DbName"=>'数据库名称',
            "{$type}TablePrefix"=>'表名前缀',
        );

        return parent::getData();
    }


    
    public function attributeLabels()
    {         return $this->attributeLabels;
    }



    
    public function getUserDbSource()
    {         $user_db_source = $this->getOptionValue("user_db_source");
        if ($user_db_source == ""){
            return "owner";
        }
        return $user_db_source;
    }
}